/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package presenter;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author alamarre
 */
public class SimpleVariableFile {

    File file;
    ArrayList<String> var=new ArrayList<String>();
    ArrayList<String> val=new ArrayList<String>();
    
    public SimpleVariableFile() {
        
    }
    
    public SimpleVariableFile(String s) {
        this (new File(s));
    }
    
    /*public void writeSchedule(String[] input) {
        for(int i=0; i<input.length; i++) {
            var.add("Song"+i);
            val.add(input[i]);
        }
    }
    
    public void writeSchedule(String[] vars, String[] input) {
        variables=new String[input.length][2];
        for(int i=0; i<input.length; i++) {
            variables[i][0]=vars[i];
            variables[i][1]=input[i];
        }
    }*/
    
    public SimpleVariableFile(File f) {
        int index=0;
        file=f;
        try {
            BufferedReader br=new BufferedReader(new FileReader(f));
            
            
            for(int i=0; i<65535; i++) {
                String temp=br.readLine();
                if(temp==null) break;
                Scanner sc=new Scanner(temp);
                sc.useDelimiter("\\s*=\\s*");
                if(sc.hasNext())
                    var.add(sc.next());
                if(sc.hasNext()) {
                    val.add(sc.next());
                     index++;
                }
               
            }
           
//            java.util.Arrays.sort(variables);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public int getNumberOfVariables() {
        return var.size();
    }
    
    public boolean exists(String input) {
        try {
            if(getVariableIndex(input)>=0)
                return true;
        } catch(Exception e) {
            return false;
        }
            
        return false;
    }
    
    public String getVariable(String input) {
       //System.out.println(variables.length);
       //System.out.println(input);
        for(int i=0; i<var.size(); i++) {
           //System.out.println(variables[i][0]);
            if(var.get(i).equals(input)) {
                return val.get(i);
            }
        }
        return "-1";
    }
    
    public String getVariable(String input,int start, int end) {
        
        int current=(start+end)/2;
        //System.out.println(input+" "+variables[current][0]);
        int compare=input.compareTo(var.get(current));
        if(compare==0) return val.get(current);
        if(current==start||current==end) return "-1";
        if(compare>0){
            return getVariable(input, current,end);
        }
        
        return getVariable(input,start,current);
    }
    
    public int getVariableIndex(String input) {
        for(int i=0; i<var.size(); i++) {
            if(var.get(i).equals(input)) {
                return i;
            }
        }
        return -1;
    }
    
    public int getVariableIndex(String input,int start, int end) {
        int current=(start+end)/2;
        int compare=input.compareTo(var.get(current));
        if(compare==0) return current;
        
        if(start>end) return -1;
        if(compare>0) return getVariableIndex(input, current,end);
        
        return getVariableIndex(input,start,current);
    }
    
    
    public void update(String[] variable, String[] value) {
        for(int i=0; i<variable.length; i++) {
            update(variable[i],value[i]);
        }
    }

    public void update(String variable, String value) {
        int pos=getVariableIndex(variable);
        if(pos<0)
            add(variable,value);
        else 
            val.set(pos, value);
    }
    
    public void add(String variable, String value) {
        var.add(variable);
        val.add(value);
        System.out.println("value added");
    }
    
    public void write(File file) {
        try{
            BufferedWriter bw=new BufferedWriter(new FileWriter(file));
            for(int i=0; i<var.size(); i++) {
                bw.write(var.get(i));
                bw.write(" = ");
                bw.write(val.get(i));
                bw.newLine();
            }
            bw.close();
        } catch( Exception e) {
            e.printStackTrace();
        }
    }
    
    public void write() {
        write(file);
    }
    
    public static void main (String[] args) {
        //System.out.println(new File(".").getAbsoluteFile());
        SimpleVariableFile svf=new SimpleVariableFile(new File("FramePositions.txt"));
//        //System.out.println(svf.getVariable("SongDisplayer1X"));
    }
}
